//Documentation complete
/**
 * @author Kazu
 * @version 2009MR11 
 */

package broker;

import problemDomain.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import static problemDomain.ErrorBean.*;
import static utilities.MySQLConnection.*;

import java.io.File;
import java.util.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

/**
 * A broker for the XML configuration file.
 */
public class ConfigFileBroker {
		
	/* Singleton Constructor */
	
	private static ConfigFileBroker broker;
	
	private ConfigFileBroker() {}
	
	/**
	 * Returns an instance of <code>ConfigFileBroker</code>.
	 * @return A reference to <code>ConfigFileBroker</code>
	 */
	public synchronized static ConfigFileBroker getBroker () {
		if (broker == null)
			return new ConfigFileBroker();
		return broker;
	}
	
	/* END Singleton Constructor */
	
	
	/* Methods */
	
	/**
	 * Lists all elements of a type from the configuration file.
	 * @param type the type of list to generate
	 * @return a <code>List</code> of all elements
	 * @throws ErrorBean if the configuration file is not found
	 */
	public synchronized List<String> listAll (String type) throws ErrorBean {
		List<String> list = new ArrayList<String>();
		
		File file = new File(CONFIG_FILE);
		
		try {
			if (type.equalsIgnoreCase("deliverystatus")) {
				
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("deliverystatus");
				
				for (int i = 0; i < nodes.getLength(); i++) {
					// pull out delivery status from xml file
					Element deliveryStatus = (Element)nodes.item(i);
		            
		            // get name
		            NodeList names = deliveryStatus.getElementsByTagName("name");
		            Element nameE = (Element) names.item(0);
		            String name = getCharacterDataFromElement(nameE);
		            
		            list.add(name);
				}
					
				
			}
			else if (type.equalsIgnoreCase("dwellingtype")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("dwelling");
				
				for (int i = 0; i < nodes.getLength(); i++) {
					// pull out dwelling type from xml file
					Element dwelling = (Element)nodes.item(i);
		            
		            // get name
		            NodeList names = dwelling.getElementsByTagName("name");
		            Element nameE = (Element) names.item(0);
		            String name = getCharacterDataFromElement(nameE);
		            
		            list.add(name);
				}
			
			}
			else if (type.equalsIgnoreCase("operationHour")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("operationhour");
				
				for (int i = 0; i < nodes.getLength(); i++) {
					// pull out operation hour from xml file
					Element opHour = (Element)nodes.item(i);
		            
		            // get the start time
		            Element startE = (Element) opHour.getElementsByTagName("start").item(0);
		            String start = getCharacterDataFromElement(startE);
		            
		            // get end time
		            Element endE = (Element) opHour.getElementsByTagName("end").item(0);
		            String end = getCharacterDataFromElement(endE);
		            
		            list.add(start);
		            list.add(end);
				}
	
			}
			else if (type.equalsIgnoreCase("orderStatus")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("orderstatus");
				
				for (int i = 0; i < nodes.getLength(); i++) {
					// pull out order status from xml file
					Element orderStatus = (Element)nodes.item(i);
		            
		            // get the status
					int length = orderStatus.getElementsByTagName("status").getLength();
					for (int j = 0; j < length; j++) {
			            Element statusE = (Element) orderStatus.getElementsByTagName("status").item(j);
			            String status = getCharacterDataFromElement(statusE);
			            list.add(status);
					}
				}
					
			}
			else if (type.equalsIgnoreCase("paymentStatus")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("paymentstatus");
				
				for (int i = 0; i < nodes.getLength(); i++) {
					// pull out payment status from xml file
					Element paymentStatus = (Element)nodes.item(i);
		            
		            // get the status
					int length = paymentStatus.getElementsByTagName("status").getLength();
					for (int j = 0; j < length; j++) {
			            Element statusE = (Element) paymentStatus.getElementsByTagName("status").item(j);
			            String status = getCharacterDataFromElement(statusE);
			            list.add(status);
					}
				}
			}
			else if (type.equalsIgnoreCase("tax")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("tax");
				
				for (int i = 0; i < nodes.getLength(); i++) {
					// pull out tax from xml file
					Element opHour = (Element)nodes.item(i);
		            
		            // get the GST
		            Element startE = (Element) opHour.getElementsByTagName("gst").item(0);
		            String start = getCharacterDataFromElement(startE);
		            
		            // get end PST
		            Element endE = (Element) opHour.getElementsByTagName("pst").item(0);
		            String end = getCharacterDataFromElement(endE);
		            
		            list.add(start);
		            list.add(end);
				}
			}
			else if (type.equalsIgnoreCase("password")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("password");
				String pass = getCharacterDataFromElement((Element)nodes.item(0));
				list.add(pass);
			}
			else if (type.equalsIgnoreCase("username")) {
				DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName("username");
				String uName = getCharacterDataFromElement((Element)nodes.item(0));
				list.add(uName);
			}
			
		} catch (Exception e) {
			throwError(
				BROKER_CONFIGFILE_NOTFOUND,
				e.getMessage(),
				this);
		}
		return list;
	}
	
	/* END Methods */

	
	/* Private Method */
	
	/**
	 * Returns the string that the passed element holds.
	 * @param e An element to be converted to string 
	 * @return the string that the passed element holds
	 */
	public static String getCharacterDataFromElement (Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData)child;
            return cd.getData();
        }
        return "?";
    }
	
	/* END Private Method */
	
}
